package com.atguigu.gulimall.member.service.impl;

import com.atguigu.common.exception.RRException;
import com.atguigu.gulimall.member.dao.TestUserMapper;
import com.atguigu.gulimall.member.entity.TestUser;
import com.atguigu.gulimall.member.service.TestUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class TestUserServiceImpl implements TestUserService {

    @Resource
    private TestUserMapper testUserMapper;

    @Override
    public int insert(TestUser record) {
        return testUserMapper.insert(record);
    }

    @Override
    public int updateByPrimaryKey(TestUser record) {
        Integer residue ;
        TestUser testUser = testUserMapper.selectById(record.getId());
        if((residue = testUser.getTotal() - record.getUse())<0)
            throw new RRException("用户余额不足");
        testUser.setResidue(residue);
        testUser.setUse(record.getUse());
        return testUserMapper.updateByPrimaryKey(testUser);
    }
}


